package com.dosh;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

/**
 * This activity enables the Change of Informations of a Budget, Category or a Item
 */
public class Admin_Change extends Activity implements OnClickListener{
	
	TextView name_text, amount_text, description_text, reason_text, actual_text, category_text;
	TextView name, amount, description, reason, actual;
	Spinner category;
	Button change, delete, new_e;
	String sort, entry;
	int id;
	DBAdapter db = new DBAdapter(this);
	ArrayAdapter<String> adapterForSpinner;
	
	/**
	 * This method loads all Buttons and Textfields.
	 */
	public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.admin_change);
        
      //Connects the variable with its field
       name_text = (TextView) findViewById(R.id.admin_change_name_text);
       amount_text = (TextView) findViewById(R.id.admin_change_amount_text);
       description_text  = (TextView) findViewById(R.id.admin_change_description_text);
       reason_text  = (TextView) findViewById(R.id.admin_change_reason_text);
       actual_text  = (TextView) findViewById(R.id.admin_change_actual_text);
       category_text  = (TextView) findViewById(R.id.admin_change_category_text);
        
       name = (TextView) findViewById(R.id.admin_change_name_field);
       amount = (TextView) findViewById(R.id.admin_change_amount_field);
       description  = (TextView) findViewById(R.id.admin_change_description_field);
       reason  = (TextView) findViewById(R.id.admin_change_reason_field);
       actual  = (TextView) findViewById(R.id.admin_change_actual_field);
       category  = (Spinner) findViewById(R.id.admin_change_category_field);
       
       change = (Button) findViewById(R.id.admin_change_change);
       delete = (Button) findViewById(R.id.admin_change_delete);
       new_e = (Button) findViewById(R.id.admin_change_new);
       
       change.setOnClickListener(this);
       delete.setOnClickListener(this);
       new_e.setOnClickListener(this);
       
       //Loads the extra informations from the previous Activity
       Bundle b = getIntent().getExtras();
       entry = b.getString("entry");
       sort = b.getString("sort");
       id = b.getInt("number");
       this.generateFields();

       
    }
	/**
	 * This method generates and fills the correct fields
	 */
	public void generateFields(){
		//Makes the needed fields visible
		if(entry.equals("budget")){
			name_text.setVisibility(View.VISIBLE);
		    amount_text.setVisibility(View.VISIBLE);
		    description_text.setVisibility(View.VISIBLE);
		    reason_text.setVisibility(View.VISIBLE);
		    actual_text.setVisibility(View.VISIBLE);
		    category_text.setVisibility(View.GONE);
			
			name.setVisibility(View.VISIBLE);
		    amount.setVisibility(View.VISIBLE);
		    description.setVisibility(View.VISIBLE);
		    reason.setVisibility(View.VISIBLE);
		    actual.setVisibility(View.VISIBLE);
		    category.setVisibility(View.GONE);
		    
		    actual.setEnabled(false);
		    
		    if(sort.equals("change")){
		    	//Fills the field with the Data of the current Budget
			    name.setText(MainActivity.currentUser.Budgets[id].name);
			    amount.setText(""+MainActivity.currentUser.Budgets[id].amount);
			    description.setText(MainActivity.currentUser.Budgets[id].description);
			    reason.setText(MainActivity.currentUser.Budgets[id].reason);
			    actual.setText(""+MainActivity.currentUser.Budgets[id].actual_amount);
		    }
		    
		}else if(entry.equals("category")){
			name_text.setVisibility(View.VISIBLE);
			amount_text.setVisibility(View.GONE);
		    description_text.setVisibility(View.GONE);
		    reason_text.setVisibility(View.GONE);
		    actual_text.setVisibility(View.GONE);
		    category_text.setVisibility(View.GONE);
			
			name.setVisibility(View.VISIBLE);
			amount.setVisibility(View.GONE);
		    description.setVisibility(View.GONE);
		    reason.setVisibility(View.GONE);
		    actual.setVisibility(View.GONE);
		    category.setVisibility(View.GONE);
		    
			if(sort.equals("change")){
				//Fills the field with the Data of the current Category
				name.setText(MainActivity.currentUser.Categories[id].name);
			}
			
		}else if(entry.equals("item")){
			this.setSpinners();
			
			name_text.setVisibility(View.VISIBLE);
			amount_text.setVisibility(View.GONE);
		    description_text.setVisibility(View.GONE);
		    reason_text.setVisibility(View.GONE);
		    actual_text.setVisibility(View.GONE);
			category_text.setVisibility(View.VISIBLE);
			
			name.setVisibility(View.VISIBLE);
			amount.setVisibility(View.GONE);
		    description.setVisibility(View.GONE);
		    reason.setVisibility(View.GONE);
		    actual.setVisibility(View.GONE);
		    category.setVisibility(View.VISIBLE);

			if(sort.equals("change")){
				//Fills the field with the Data of the current Item
				name.setText(MainActivity.currentUser.Items[id].name);
				category.setSelection(adapterForSpinner.getPosition(MainActivity.currentUser.Items[id].category.name));
				
			}
		}
		//sets the needet buttons
		if(sort.equals("change")){
			change.setVisibility(View.VISIBLE);
			delete.setVisibility(View.VISIBLE);
			new_e.setVisibility(View.GONE);
		}
		else if(sort.equals("new")){
			change.setVisibility(View.GONE);
			delete.setVisibility(View.GONE);
			new_e.setVisibility(View.VISIBLE);
		}
		
	}
	/**
	 * This method sets the selection of the spinners for the Category.
	 */
	public void setSpinners(){
		//sets the content of the Category spinner
		db.open();
		
		adapterForSpinner = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item); 
        adapterForSpinner.setDropDownViewResource (android.R.layout.simple_spinner_dropdown_item); 
        
        category.setAdapter(adapterForSpinner);
        Category[] a=MainActivity.currentUser.Categories;
        for(int i = 0; i<a.length; i++){
        	adapterForSpinner.add(a[i].name);
        }
        db.close();
	}
	
	/**
	 * This method handles the OnClickListener.
	 */
	public void onClick(View v) {
		if(v==new_e){
			//creates a new Budget, Category or Item an updates the current User
			if(entry.equals("budget")){
				MainActivity.currentUser.createBuget(name.getText().toString(), Integer.parseInt(amount.getText().toString()),
						description.getText().toString(), reason.getText().toString(), db);
				MainActivity.currentUser.updateBudget(db);
			}
			else if(entry.equals("category")){
				MainActivity.currentUser.createCategory(name.getText().toString(), db);
				MainActivity.currentUser.updateCategory(db);
			}
			else if(entry.equals("item")){
				MainActivity.currentUser.createItem(name.getText().toString(),
				MainActivity.currentUser.createCategory(category.getSelectedItem().toString(), db), db);
				MainActivity.currentUser.updateItem(db);
			}
			//Makes a Message an leads back to the Administration-Activity
			Toast.makeText(Admin_Change.this, "New Entry generated", Toast.LENGTH_LONG).show();
			this.finish();
		}
		else if(v==delete){
			//deletes the selected Budget, Category or Item an updates the current User
			db.open();
			if(entry.equals("budget")){
				db.delete("Budget",MainActivity.currentUser.Budgets[id].budget_ID);
				MainActivity.currentUser.updateBudget(db);
				
			}else if(entry.equals("category")){
				db.delete("Category", MainActivity.currentUser.Categories[id].category_ID);
				MainActivity.currentUser.updateCategory(db);
				
			}else if(entry.equals("item")){
				db.delete("Item", MainActivity.currentUser.Items[id].item_ID);
				MainActivity.currentUser.updateItem(db);
			}
			db.close();
			//Makes a Message an leads back to the Administration-Activity
			Toast.makeText(Admin_Change.this, "Entry deleted", Toast.LENGTH_LONG).show();
			this.finish();
		}
		else if(v==change){
			//updates the selected Budget, Category or Item
			if(entry.equals("budget")){
				MainActivity.currentUser.Budgets[id].name=name.getText().toString();
				MainActivity.currentUser.Budgets[id].amount=Integer.parseInt(amount.getText().toString());
				MainActivity.currentUser.Budgets[id].description=description.getText().toString();
				MainActivity.currentUser.Budgets[id].reason=reason.getText().toString();
				MainActivity.currentUser.Budgets[id].user_ID=MainActivity.currentUser.user_ID;
				MainActivity.currentUser.Budgets[id].update(db);
				
			}else if(entry.equals("category")){
				MainActivity.currentUser.Categories[id].name=name.getText().toString();
				MainActivity.currentUser.Categories[id].User_ID=MainActivity.currentUser.user_ID;
				MainActivity.currentUser.Categories[id].update(db);
				
			}else if(entry.equals("item")){
				MainActivity.currentUser.Items[id].name=name.getText().toString();
				MainActivity.currentUser.Items[id].category=MainActivity.currentUser.createCategory(category.getSelectedItem().toString(), db);
				MainActivity.currentUser.Items[id].update(db);
				
			}
			//Makes a Message an leads back to the Administration-Activity
			Toast.makeText(Admin_Change.this, "Entry changed", Toast.LENGTH_LONG).show();
			this.finish();
		}
		
	}

    
    
}
